const { getCourseData, changeField, changeCourseStatus } = require('../services/Course'),
      { getCourseFieldData } = require('../services/CourseTab'),
      { getRecomCourseData, changeRecomCourseStatus }  = require('../services/RecomCourse'),
      { getSliderData, changeSliderStatus } = require('../services/Slider'),
      { getCollectionData, changeCollectionStatus } = require('../services/Collection'),
      { getTeacherData, changeTeacherStatus, selectStarTeacher } = require('../services/Teacher'),
      { getStudentData, changeStudentStatus } = require('../services/Student'),
      { returnInfo } = require('../libs/utils'),
      { API } = require('../config/error_config');


class Index {
    //课程管理
    async getCourses(ctx, next) {
 
        const courseData = await getCourseData(),
              fieldData = await getCourseFieldData();
        ctx.body = courseData && fieldData
                 ? returnInfo(API.RETURN_SUCCESS, {
                     courseData,
                     fieldData
                 })
                 : returnInfo(API.RETURN_FAILED);
         
    }

    //推荐课程
    async getRecomCourses (ctx, next) {
        const data = await getRecomCourseData();
        
        ctx.body = data
                 ? returnInfo(API.RETURN_SUCCESS,data)
                 : returnInfo(API.RETURN_FAILED);
      
    }
    //轮播图管理
    async getSliders (ctx, next) {
       const data = await  getSliderData();
       ctx.body = data 
                ? returnInfo(API.RETURN_SUCCESS,data)
                : returnInfo(API.RETURN_FAILED);
    }
  //课程集合
   async getCollections (ctx, next) {
    const data = await  getCollectionData();
    ctx.body = data 
             ? returnInfo(API.RETURN_SUCCESS,data)
             : returnInfo(API.RETURN_FAILED);
   }
  //老师数据获取与设置
  async getTeachers (ctx, next) {
    const data = await  getTeacherData();
    ctx.body = data 
             ? returnInfo(API.RETURN_SUCCESS,data)
             : returnInfo(API.RETURN_FAILED);
  }

  //学生数据的获取与设置
  async getStudents (ctx, next) {
    const data = await getStudentData();
    ctx.body = data 
             ? returnInfo(API.RETURN_SUCCESS,data)
             : returnInfo(API.RETURN_FAILED);
  }

    async changeCourseField(ctx, next) {

        const { cid, field } = ctx.request.body;
        const result = await changeField(cid, field);

        if (!result) {
            ctx.body = returnInfo(API.CHANGE_COURSE_FIELD_FAILED);
            return;
        }
        
        ctx.body = returnInfo(API.CHANGE_COURSE_FIELD_SUCCESS)
    }
    //更改上下架设置
    async changeDataStatus (ctx, next) {
        const { id, status, field } = ctx.request.body;
        let result = null;
    
        switch (field) {
          case 'COURSE':
            result = await changeCourseStatus(id, status);  
            break;
          case 'RECOM_COURSE':
            result = await changeRecomCourseStatus(id, status);
            break;
          case 'SLIDER':
              result = await changeSliderStatus(id, status);
              break;
          case 'COLLECTION':
                result = await changeCollectionStatus(id, status);
                break;
          case 'TEACHER':
                result = await changeTeacherStatus(id, status);
                break;
          case 'STUDENT':
                result = await changeStudentStatus(id, status);
                break;
          default:
            ctx.body = returnInfo(API.FIELD_ERROR);
            return;
        }
    
        if (!result) {
          ctx.body = returnInfo(API.CHANGE_STATUS_FAILED);
          return;
        }
    
        ctx.body = returnInfo(API.CHANGE_STATUS_SUCCESS);
    }
    //设置明星老师
    async changeTeacherStar (ctx, next) {
      const { id, isStar } = ctx.request.body;
      const result = await selectStarTeacher(id, isStar);
      
      if (!result) {
          ctx.body = returnInfo(API.SELECT_STAR_TEACHER_FAILED);
          return;
      }
      
      ctx.body = returnInfo(API.SELECT_STAR_TEACHER_SUCCESS);
    }
}

module.exports = new Index();